package com.oa.modules.approvalsec1.controller;


import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.oa.modules.approvalsec1.entity.BiddEntity;
import com.oa.modules.approvalsec1.entity.BiddProjectEntity;
import com.oa.modules.approvalsec1.entity.InstitutionsEntity;
import com.oa.modules.approvalsec1.entity.JointsEntity;
import com.oa.modules.approvalsec1.service.BiddProjectService;
import com.oa.modules.approvalsec1.service.BiddService;
import com.oa.modules.approvalsec1.service.InstitutionsService;
import com.oa.modules.approvalsec1.service.JointsService;
import com.oa.modules.sys.controller.AbstractController;
import com.oa.modules.sys.entity.SysUserEntity;
import com.oa.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
import org.springframework.web.bind.annotation.*;


/**
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2022-05-05 17:01:41
 */
@RestController
@RequestMapping("/bidd")
@Api(tags = "相关接口")
public class BiddController extends AbstractController {
    @Autowired
    private BiddService biddService;
    @Autowired
    private InstitutionsService institutionsService;
    @Autowired
    private JointsService jointsService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = biddService.queryPage(params);
        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") Integer id) {
        BiddEntity bidd = biddService.getById(id);
        return R.ok().put("bidd", bidd);
    }

    /**
     *保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody BiddEntity bidd) {
        if(StringUtils.isNotEmpty(String.valueOf(bidd.getJoint()))){
            JointsEntity jointsEntity = jointsService.getById(bidd.getJoint());
            if(jointsEntity!=null){
                bidd.setJointName(jointsEntity.getName());
            }
        }
        bidd.setCreateDate(new Date());
        biddService.save(bidd);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody BiddEntity bidd) {
        UpdateWrapper<BiddEntity> wrapper = new UpdateWrapper<BiddEntity>().eq("project_id", bidd.getProjectId()).eq("joint",bidd.getJoint());
        wrapper.set("money",bidd.getMoney());
        biddService.update(wrapper);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody Integer[] ids) {
        biddService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
